package com.vsu.bookstore.mvc.model.bean;

import java.text.DecimalFormat;

import com.vaadin.addon.sqlcontainer.RowId;
import com.vaadin.data.Item;
import com.vsu.bookstore.util.CurrencyExchangeProvider;
import com.vsu.bookstore.util.DatabaseHelper;


public class BookStoreItem
{
  private Integer id;
  private Double price;
  private String title;
  private Integer categoryId;
  private String description;
  private byte[] image;
  private Integer quantity = 1;

  public BookStoreItem()
  {
  }

  @Override
  public boolean equals(final Object obj)
  {
    if (this == obj)
      return true;
    if (obj == null || getClass() != obj.getClass())
      return false;
    return getId().equals(((BookStoreItem) obj).getId());
  }

  public Integer getCategoryId()
  {
    return categoryId;
  }

  public String getCategoryTitle()
  {
    final Item item = DatabaseHelper.get().getCategoryContainer().getItem(new RowId(new Object[]{categoryId}));
    if (item == null)
      return "";
    return item.getItemProperty("Title").getValue().toString();
  }

  public String getDescription()
  {
    return description;
  }

  public Integer getId()
  {
    return id;
  }

  public byte[] getImage()
  {
    return image;
  }

  public Double getPrice()
  {
    return price;
  }

  public String getPriceInRubles()
  {
    final DecimalFormat format = new DecimalFormat();
    format.setMaximumFractionDigits(2);
    return format.format(price * CurrencyExchangeProvider.getEuroToRubleRatio());
  }

  public Integer getQuantity()
  {
    return quantity;
  }

  public String getTitle()
  {
    return title;
  }

  @Override
  public int hashCode()
  {
    int result = 17;
    result = 37 * result +getId().hashCode();
    result = 37 * result +getClass().getName().hashCode();
    return result;
  }

  public void increaseQuantity(final int inc)
  {
    quantity += inc;
  }

  public void setCategoryId(final Integer categoryId)
  {
    this.categoryId = categoryId;
  }

  public void setDescription(final String description)
  {
    this.description = description;
  }

  public void setId(final Integer id)
  {
    this.id = id;
  }

  public void setImage(final byte[] image)
  {
    this.image = image;
  }

  public void setPrice(final Double price)
  {
    this.price = price;
  }

  public void setQuantity(final Integer quantity)
  {
    this.quantity = quantity;
  }

  public void setTitle(final String title)
  {
    this.title = title;
  }
}
